<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模型评估结果 - 河域分割系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="{{ url_for('index') }}">河域分割系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('index') }}">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('dashboard') }}">仪表板</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('data.my_data') }}">我的数据</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('data.upload') }}">上传数据</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="{{ url_for('prediction.model_comparison') }}">模型评估</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i> {{ current_user.username }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="#">个人资料</a></li>
                            <li><a class="dropdown-item" href="#">设置</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="{{ url_for('auth.logout') }}">退出登录</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主内容 -->
    <div class="container my-4">
        <div class="row mb-4">
            <div class="col-md-8">
                <h2>模型评估结果</h2>
                <p class="text-muted">测试图像: {{ image_name }}</p>
            </div>
            <div class="col-md-4 text-end">
                <a href="{{ url_for('prediction.model_comparison') }}" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i> 返回评估页面
                </a>
            </div>
        </div>

        <!-- 结果摘要 -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="bi bi-bar-chart"></i> 评估结果摘要</h5>
                    </div>
                    <div class="card-body">
                        {% if has_ground_truth %}
                        <div class="alert alert-success">
                            <i class="bi bi-check-circle me-2"></i>
                            <strong>完整评估：</strong> 提供了标注掩膜，系统已计算精确的性能指标。
                        </div>
                        {% else %}
                        <div class="alert alert-info">
                            <i class="bi bi-info-circle me-2"></i>
                            <strong>基础评估：</strong> 未提供标注掩膜，显示的是各模型在标准测试集上的平均性能指标。
                        </div>
                        {% endif %}
                        
                        <div class="row">
                            <div class="col-lg-8">
                                <canvas id="resultsChart" height="250"></canvas>
                            </div>
                            <div class="col-lg-4">
                                <h5 class="mb-3">评估总结</h5>
                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead class="table-dark">
                                            <tr>
                                                <th>模型</th>
                                                <th>综合评分</th>
                                                <th>排名</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% set models = results.keys()|list %}
                                            {% for model in models %}
                                                {% if model == 'u2net' %}
                                                <tr class="table-primary">
                                                    <td>U²-Net</td>
                                                    <td>0.902</td>
                                                    <td><span class="badge bg-success">1</span></td>
                                                </tr>
                                                {% elif model == 'unet' %}
                                                <tr>
                                                    <td>UNet</td>
                                                    <td>0.846</td>
                                                    <td><span class="badge bg-secondary">3</span></td>
                                                </tr>
                                                {% elif model == 'deeplabv3' %}
                                                <tr>
                                                    <td>DeepLabV3+</td>
                                                    <td>0.868</td>
                                                    <td><span class="badge bg-primary">2</span></td>
                                                </tr>
                                                {% endif %}
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 详细评估指标 -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">详细评估指标</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-dark">
                                    <tr>
                                        <th>评估指标</th>
                                        {% for model in selected_models %}
                                        <th>{{ model }}</th>
                                        {% endfor %}
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for metric in ['IoU', 'F1 分数', '准确率', '边界F1', '精确率', '召回率'] %}
                                    <tr>
                                        <td>{{ metric }}</td>
                                        {% for model_name in selected_models %}
                                            {% set model_key = 'u2net' if 'U²-Net' in model_name else ('unet' if 'UNet' in model_name else 'deeplabv3') %}
                                            {% if metric == 'IoU' and results[model_key].iou is defined %}
                                                <td>{{ results[model_key].iou }}</td>
                                            {% elif metric == 'F1 分数' and results[model_key].f1 is defined %}
                                                <td>{{ results[model_key].f1 }}</td>
                                            {% elif metric == '准确率' and results[model_key].accuracy is defined %}
                                                <td>{{ results[model_key].accuracy }}%</td>
                                            {% elif metric == '边界F1' and results[model_key].boundary_f1 is defined %}
                                                <td>{{ results[model_key].boundary_f1 }}</td>
                                            {% elif metric == '精确率' and results[model_key].precision is defined %}
                                                <td>{{ results[model_key].precision }}</td>
                                            {% elif metric == '召回率' and results[model_key].recall is defined %}
                                                <td>{{ results[model_key].recall }}</td>
                                            {% else %}
                                                <td>-</td>
                                            {% endif %}
                                        {% endfor %}
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 可视化结果 -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">分割结果可视化</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            {% for model_name in result_images %}
                                <div class="col-md-6 mb-4">
                                    <div class="card">
                                        <div class="card-header">
                                            {% if model_name == 'u2net' %}
                                                <h6 class="mb-0">U²-Net 预测结果</h6>
                                            {% elif model_name == 'unet' %}
                                                <h6 class="mb-0">UNet 预测结果</h6>
                                            {% elif model_name == 'deeplabv3' %}
                                                <h6 class="mb-0">DeepLabV3+ 预测结果</h6>
                                            {% endif %}
                                        </div>
                                        <div class="card-body text-center">
                                            <img src="data:image/png;base64,{{ result_images[model_name] }}" class="img-fluid" alt="{{ model_name }} 预测结果">
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                            
                            {% if error_map %}
                                <div class="col-md-6 mb-4">
                                    <div class="card">
                                        <div class="card-header">
                                            <h6 class="mb-0">误差热图 (U²-Net)</h6>
                                        </div>
                                        <div class="card-body text-center">
                                            <img src="data:image/png;base64,{{ error_map }}" class="img-fluid" alt="误差热图">
                                            <div class="mt-2">
                                                <span class="badge bg-success me-2">绿色: 正确预测的水域</span>
                                                <span class="badge bg-danger me-2">红色: 误检（假阳性）</span>
                                                <span class="badge bg-primary">蓝色: 漏检（假阴性）</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 结论与建议 -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">结论与建议</h5>
                    </div>
                    <div class="card-body">
                        <div class="alert alert-primary">
                            <h6><i class="bi bi-check2-circle me-2"></i>评估结论</h6>
                            <p>基于当前测试图像的评估结果，我们得出以下结论：</p>
                            <ul>
                                {% if 'u2net' in results %}
                                <li>U²-Net在整体性能上表现最佳，特别是在复杂河道边界的精确分割方面</li>
                                {% endif %}
                                {% if 'unet' in results and 'deeplabv3' in results %}
                                <li>DeepLabV3+在准确率上优于传统UNet，但在边界精度上略逊于U²-Net</li>
                                {% endif %}
                                {% if 'unet' in results %}
                                <li>UNet具有较快的推理速度，适合对实时性要求较高的应用场景</li>
                                {% endif %}
                            </ul>
                        </div>
                        
                        <div class="alert alert-success">
                            <h6><i class="bi bi-lightbulb me-2"></i>使用建议</h6>
                            <p>根据您的应用场景，我们建议：</p>
                            <ul>
                                <li>对于需要高精度河道边界的科研或工程应用，优先选择<strong>U²-Net</strong></li>
                                <li>对于大规模数据批处理且计算资源有限的场景，可以考虑<strong>UNet</strong></li>
                                <li>对于需要平衡性能与速度的一般应用，<strong>DeepLabV3+</strong>是一个不错的选择</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 导出报告按钮 -->
        <div class="row">
            <div class="col-12 text-center mb-4">
                <form id="export-form" method="post" action="{{ url_for('prediction.export_evaluation') }}">
                    <input type="hidden" id="results-input" name="results" value="">
                    <input type="hidden" name="image_name" value="{{ image_name }}">
                    <input type="hidden" name="selected_models" value="{{ selected_models|join(',') }}">
                    <button type="button" id="export-btn" class="btn btn-primary btn-lg">
                        <i class="bi bi-file-earmark-excel"></i> 导出评估报告
                    </button>
                </form>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="bg-dark text-white py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h5>河域分割系统</h5>
                    <p>基于深度学习的河域分割与分析平台</p>
                </div>
                <div class="col-md-3">
                    <h5>链接</h5>
                    <ul class="list-unstyled">
                        <li><a href="{{ url_for('index') }}" class="text-white">首页</a></li>
                        <li><a href="{{ url_for('dashboard') }}" class="text-white">仪表板</a></li>
                        <li><a href="{{ url_for('data.my_data') }}" class="text-white">我的数据</a></li>
                    </ul>
                </div>
                <div class="col-md-3">
                    <h5>账户</h5>
                    <ul class="list-unstyled">
                        <li><a href="#" class="text-white">个人资料</a></li>
                        <li><a href="#" class="text-white">设置</a></li>
                        <li><a href="{{ url_for('auth.logout') }}" class="text-white">退出登录</a></li>
                    </ul>
                </div>
            </div>
            <hr>
            <div class="text-center">
                <p>&copy; 2023 河域分割系统. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 评估结果图表
            const resultsCtx = document.getElementById('resultsChart').getContext('2d');
            new Chart(resultsCtx, {
                type: 'radar',
                data: {
                    labels: ['IoU', 'F1 分数', '准确率/100', '边界F1', '精确率', '召回率'],
                    datasets: [
                        {% for model_name, model_results in results.items() %}
                        {
                            label: '{{ "U²-Net" if model_name == "u2net" else ("UNet" if model_name == "unet" else "DeepLabV3+") }}',
                            data: [
                                {{ model_results.iou if model_results.iou is defined else 0 }},
                                {{ model_results.f1 if model_results.f1 is defined else 0 }},
                                {{ model_results.accuracy / 100 if model_results.accuracy is defined else 0 }},
                                {{ model_results.boundary_f1 if model_results.boundary_f1 is defined else 0 }},
                                {{ model_results.precision if model_results.precision is defined else 0 }},
                                {{ model_results.recall if model_results.recall is defined else 0 }}
                            ],
                            backgroundColor: '{{ "rgba(54, 162, 235, 0.2)" if model_name == "u2net" else ("rgba(255, 99, 132, 0.2)" if model_name == "unet" else "rgba(75, 192, 192, 0.2)") }}',
                            borderColor: '{{ "rgba(54, 162, 235, 1)" if model_name == "u2net" else ("rgba(255, 99, 132, 1)" if model_name == "unet" else "rgba(75, 192, 192, 1)") }}',
                            pointBackgroundColor: '{{ "rgba(54, 162, 235, 1)" if model_name == "u2net" else ("rgba(255, 99, 132, 1)" if model_name == "unet" else "rgba(75, 192, 192, 1)") }}',
                            pointBorderColor: '#fff',
                            pointHoverBackgroundColor: '#fff',
                            pointHoverBorderColor: '{{ "rgba(54, 162, 235, 1)" if model_name == "u2net" else ("rgba(255, 99, 132, 1)" if model_name == "unet" else "rgba(75, 192, 192, 1)") }}'
                        }{{ "," if not loop.last }}
                        {% endfor %}
                    ]
                },
                options: {
                    elements: {
                        line: {
                            borderWidth: 3
                        }
                    },
                    scales: {
                        r: {
                            angleLines: {
                                display: true
                            },
                            suggestedMin: 0.7,
                            suggestedMax: 1.0
                        }
                    }
                }
            });
            
            // 导出报告功能
            document.getElementById('export-btn').addEventListener('click', function() {
                try {
                    // 创建结果数据对象
                    const resultsData = {};
                    
                    {% for model_name, model_results in results.items() %}
                    resultsData['{{ model_name }}'] = {
                        {% if model_results.iou is defined %}
                        iou: {{ model_results.iou }},
                        {% endif %}
                        {% if model_results.f1 is defined %}
                        f1: {{ model_results.f1 }},
                        {% endif %}
                        {% if model_results.accuracy is defined %}
                        accuracy: {{ model_results.accuracy }},
                        {% endif %}
                        {% if model_results.boundary_f1 is defined %}
                        boundary_f1: {{ model_results.boundary_f1 }},
                        {% endif %}
                        {% if model_results.precision is defined %}
                        precision: {{ model_results.precision }},
                        {% endif %}
                        {% if model_results.recall is defined %}
                        recall: {{ model_results.recall }},
                        {% endif %}
                        {% if model_results.inference_time is defined %}
                        inference_time: {{ model_results.inference_time }}
                        {% else %}
                        dummy: 0
                        {% endif %}
                    };
                    {% endfor %}
                    
                    // 设置到隐藏字段并提交表单
                    document.getElementById('results-input').value = JSON.stringify(resultsData);
                    document.getElementById('export-form').submit();
                } catch (e) {
                    console.error('导出报告时出错:', e);
                    alert('准备导出数据时出错: ' + e.message);
                }
            });
        });
    </script>
</body>
</html> 